<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/6/10
 * Time: 9:44 上午
 */

namespace app\index\controller;
use think\Db;
use think\facade\Request;

class School extends Base
{
    public function index(){
        if(Request::isPost()){
            $data = input("post.");
        // dump($data);die;
            unset($data['file']);
            //功能教室
            $gongneng =[];
            foreach($data['gongnengname'] as $key=>$val){
                $gongneng[$key]['name'] = $val;
                if(!isset($data['gongneng'][$key])){
                    ajaxMsg(0,'请填写面积');
                }
                $gongneng[$key]['mianji'] = $data['gongneng'][$key];
                $gongneng[$key]['num'] = $data['gongnengnum'][$key];
            }
            unset($data['gongnengname']);unset($data['gongnengnum']);

            $data['gongneng'] = json_encode($gongneng);
            //课程信息
            $class = [];
            foreach($data['class_name'] as $key=>$val){
                $class[$key]['name'] = $val;
                if(!isset($data['class_time'][$key])||!isset($data['class_type'][$key])||!isset($data['class_teacher'][$key])||!isset($data['class_info'][$key])){
                    ajaxMsg(0,'请补全课程信息');
                }
                $class[$key]['times'] = $data['class_time'][$key];
                $class[$key]['type'] = $data['class_type'][$key];
                $class[$key]['teacher'] = $data['class_teacher'][$key];
                $class[$key]['info'] = $data['class_info'][$key];
            }
            unset($data['class_time']);unset($data['class_type']);unset($data['class_teacher']);unset($data['class_info']);unset($data['class_name']);
            $data['class_info'] = json_encode($class);
            //专职老师信息
            // $teacher = [];
            // if(isset($data['teacher_name'])){
            //     foreach($data['teacher_name'] as $key=>$val){
            //         $teacher[$key]['name'] = $val;
            //         if(!isset($data['teacher_sex'][$key])||!isset($data['teacher_year'][$key])||!isset($data['work_stime'][$key])||!isset($data['work_etime'][$key])||!isset($data['teacher_zhizheng'][$key])){
            //             ajaxMsg(0,'请补全老师信息');
            //         }
            //         $teacher[$key]['sex'] = $data['teacher_sex'][$key];
            //         $teacher[$key]['year'] = $data['teacher_year'][$key];
            //         if(strtotime($data['work_etime'][$key])<strtotime($data['work_stime'][$key])){
            //             ajaxMsg(0,'教师工作年限有误');
            //         }
            //         $teacher[$key]['work'] = $this->Sec2Time(strtotime($data['work_etime'][$key])-strtotime($data['work_stime'][$key]));
            //         $teacher[$key]['field'] = $data['teach_field'][$key];
            //         $teacher[$key]['zhizheng'] = $data['teacher_zhizheng'][$key];
            //     }
            //     unset($data['teacher_name']);unset($data['teacher_sex']);unset($data['teacher_year']);unset($data['work_stime']);unset($data['work_etime']);
            //     unset($data['teacher_zhizheng']);unset($data['teach_field']);
            //     $data['teacher_info'] = json_encode($teacher);
            // }
            
            $data['admin_id'] = $this->admin['id'];
            $data['status'] = 1;
            $data['time'] = time();
            if(Db::name("school_renzheng")->where("id",$data['id'])->find()){
                $res = Db::name("school_renzheng")->where("id",$data['id'])->update($data);
            }else{
                $res = Db::name("school_renzheng")->insert($data);
            }

            if($res){
                ajaxMsg(1,'提交成功,等待审批');
            }
            ajaxMsg(0,'网络延时稍后再试');
        }
        $info = Db::name("school_renzheng")->where('admin_id',$this->admin['id'])->find();
        $gongneng = json_decode($info['gongneng'],true);

        $this->assign('gongneng',$gongneng);

        $class = json_decode($info['class_info'],true);
        if($class){
            foreach($class as &$val){
                $val['type'] = explode(",",$val['type']);
            }
        }
        
        // dump($class);die;
        $this->assign('class',$class);
        // dump($class);die;
        $teacher = json_decode($info['teacher_info'],true);
        // dump($info);die;
        $this->assign('teacher',$teacher);
        $this->assign('info',$info);
        return $this->fetch();
    }
    private function Sec2Time($time){
        if(is_numeric($time)){
            $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            if($time >= 31556926){
                $value["years"] = floor($time/31556926);
                $time = ($time%31556926);
            }
            if($time >= 86400){
                $value["days"] = floor($time/86400);
                $time = ($time%86400);
            }
            if($time >= 3600){
                $value["hours"] = floor($time/3600);
                $time = ($time%3600);
            }
            if($time >= 60){
                $value["minutes"] = floor($time/60);
                $time = ($time%60);
            }
            $value["seconds"] = floor($time);
            //return (array) $value;
            $t=$value["years"] ."年". $value["days"] ."天"." ". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
            Return $t;

        }else{
            return (bool) FALSE;
        }
    }
    public function shenlist(){
        $pwork = input("pwork");
        $zhizheng = input('zhizheng');
        $xuexi = input('xuexi');
        $status = input("status");
        $if_teacher = input('if_teacher');
        $where = [];
        if($pwork){
            $where[] = ['pwork','=',$pwork]; 
        }
        if($xuexi){
            $where[] = ['xuexi','like','%'.$xuexi.'%']; 
        }
        if($zhizheng){
            // $zhizheng = json_encode($zhizheng);
            $where[] =  ['zhizheng','like',"%".$zhizheng."%"];
        }
        if($status){
            $where[] =  ['status','=',$status];
            if($status==2){
                $where[] =  ['status','>=',2];
            }
        }
        if($if_teacher){
            $where[] =  ['if_teacher','=',$if_teacher];
        }
        // dump($this->admin);
            $list = Db::name("renzheng")->where("school",$this->admin['school'])->where($where)->where("status",'>','0')->orderRaw("status=1 desc")->paginate(10)->each(function($item){
                // $item['zhizheng'] = explode(',',$item['zhizheng']);
            
                // $item['nian'] = Sec2Time($item['nian']);
                return $item;
            });
            // dump($list);die;
      
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this->fetch();
    }
    public function changestatus(){
        $id = input('id');
        
        if(is_array($id)){
            $status = input('status');
//            dump($id);
            $id_str ='';
            foreach($id as $key=>$val){
                if($key==0){
                    $id_str = $val;
                }else{
                    $id_str .=','.$val;
                }
            }
            $res = Db::name('renzheng')->where("id",'in',$id_str)->update(['status'=>$status]);
        }else{
            $res = Db::name("renzheng")->where('id',$id)->update(['status'=>2]);
        }
        ajaxMsg('1','提交成功');
    }
    public function nochangestatus(){
        $id = input('id');
        $msg = input('msg');
        $res = Db::name("renzheng")->where('id',$id)->update(['status'=>3,'school_message'=>$msg]);
        ajaxMsg('1','提交成功');
    }
    public function memberinfo(){
        $id = input('id');
        $member = Db::name("renzheng")->where("id",$id)->find();
        $work = json_decode($member['work'],true);

        $this->assign('work',$work);

        $peixun = json_decode($member['peixun'],true);

        $this->assign('peixun',$peixun);

        $this->assign('admin',$member);
        
        $xueduan = json_decode($member['xueduan'],true);
        $this->assign('xueduan',$xueduan);
        
        $zhizheng = explode(',',$member['zhizheng']);
        $this->assign('zhizheng',$zhizheng);
        // dump($zhizheng);die;
        
        $zsfield = json_decode($member['zsfield'],true);
        $this->assign("zsfield",$zsfield);
        
        $cate = Db::name("cate")->where('status',1)->where('id','in','2,52,68')->select();

        $area = Db::name("cate")->where("id",$member['area'])->find();
        $school = Db::name("cate")->where("id",$member['school'])->find();
        
        $this->assign('area',$area);
        $this->assign('school',$school);
        $this->assign('cate',$cate);
        // $this->assign('member',$member);
        return $this->fetch();
    }
    public function ketishenqing(){
        $id=input('id');
        $nian = input("nian");
        if(!$nian){
            $list = Db::name("keti")->select();
            $ary =[];
            foreach($list as $val){
                $ary[] = $val['year'];
            }
            $ary = array_unique($ary);

            $this->assign("nian",$ary);
            return $this->fetch("chosenian");
        }
        if($id==''){
            $list = Db::name("keti")->where("year",$nian)->select();
            $this->assign("list",$list);
            $this->assign('nian',$nian);
            return $this->fetch("step1");
        }
        
        $if_inportant = input("if_inportant");
        $where = [];
        $where['sq.cid'] = $id;
        if($if_inportant){
            $where['if_inportant'] = $if_inportant;
        }
        $order = input('order');
        $order_str = '';
        if(!$order){
            $order_str = 'sq.s_fen desc';
        }else{
            $order_str = 'sq.s_fen '.$order;
        }
        // dump($where);die;
          //1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
        
        if($this->admin['rid']==13){
            $list = Db::name("shenqing as sq")
                ->leftjoin('keti kt','kt.id=sq.cid')
                ->leftjoin("admin ad",'ad.id=sq.admin_id')
                ->leftjoin('cate c','c.id=ad.school')
                // ->where("sq.school_id",$this->admin['school'])
                ->where("pid",'in',[2,52])
                ->where($where)
                // ->where("ad.area","in",[2,52])
                
                ->where('sq.status','<>','0')
                ->where('sq.status','<>','-1')
                ->order($order_str)
                ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content,sq.if_inportant,ad.name as aname,sq.s_fen as fen,sq.if_inportant")
                ->paginate(10,false,['query'=>request()->param()]);
        }else{
            $list = Db::name("shenqing as sq")
                ->leftjoin('keti kt','kt.id=sq.cid')
                ->leftjoin("admin ad",'ad.id=sq.admin_id')
                ->where("sq.school_id",$this->admin['school'])
                ->where($where)
                // ->where("ad.area","in",[2,52])
                
                ->where('sq.status','<>','0')
                ->where('sq.status','<>','-1')
                ->order($order_str)
                ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content,sq.if_inportant,ad.name as aname,sq.s_fen as fen,sq.if_inportant")
                ->paginate(10,false,['query'=>request()->param()]);
        }
        // dump($list);die;
        $this->assign('page',$list->render());
        //根据学校种类.生成审核部门
        $title = [];
        if($this->admin['area']==52){
            $cate = "直属校";
        }elseif($this->admin['area']==2){
            $cate = "普通高校、高职院校";
        }else{
            $cate = "其他中小学/中职/特教学校";
        }
        
        if($cate=="普通高校、高职院校"){
            $title[0] = '学校审核';
            $title[1] = '中心审核';
            
        }else if($cate=="其他中小学/中职/特教学校"){
            $title[0] = '教委审核';
            $title[1] = '中心审核';
        }else if($cate=="直属校"){
            
            $title[0] = '学校审核';
            $title[1] = '中心审核';
        }
        // dump($title);die;
        // $this->assign('title',$title);
        $list = $list->toArray();
        $new_ary = [];
    
        foreach($list['data'] as $key=>$val){
            $new_ary[$key] = $val;
            $new_ary[$key]['content'] = json_decode($val['content'],true);
            //查看打分的数量
            $count1 = Db::name("dafen as df")
                    ->join('admin a','df.admin_id=a.id')
                    ->where('a.school',$this->admin['school'])
                    ->where("sid",$val['id'])
                    ->count();
                    
            //查看可以打分专家数量
            $count2 = Db::name("admin")->where("school",$this->admin['school'])->where("rid",12)->where("z_type",'in',['全部课题',$val['if_inportant']])->count();
            // echo $count2;die;
            if($count1==$count2){
                $new_ary[$key]['shenhe'] = 1;
            }else{
                $new_ary[$key]['shenhe'] = 0;
            }
            foreach($title as $k=>$v){
                $new_ary[$key]['statusdes'][$k] = getstatus($v,$val['status']);
            }
        }
        // dump($new_ary);die;
        if($cate!="普通高校、高职院校"&&$cate != "直属校"){
            $new_ary =[];
            $this->assign('page','');
        }
        $title[0] = '学校审核';
        $title[1] = '市中心审核';
        $s_cate = Db::name('cate')->where("id",$this->admin['school'])->find();
        $this->assign('s_cate',$s_cate);
        $this->assign('title',$title);
        $this->assign('id',$id);
        $this->assign('nian',$nian);
        $this->assign('data',$new_ary);
        return $this->fetch();
    }
    public function chakan(){
        $id = input('id');
        $type = input('type');
        
        $info = Db::name('shenqing')->where('id',$id)->find();
        $this->assign("info",$info);
        $content = json_decode($info['content'],true);
        $content['s_name'] = isset($content['s_name'])?array_values($content['s_name']):'';
        $content['s_num'] =isset($content['s_num'])?array_values($content['s_num']):'';
        $content['s_price'] = isset($content['s_price'])?array_values($content['s_price']):'';
        $content['s_year'] = isset($content['s_year'])?array_values($content['s_year']):'';
        $content['member_name'] = isset($content['member_name'])?array_values($content['member_name']):'';
        $content['member_zhiye'] = isset($content['member_zhiye'])?array_values($content['member_zhiye']):'';
        $content['member_year'] = isset($content['member_year'])?array_values($content['member_year']):'';
        $content['member_zhuanye'] = isset($content['member_zhuanye'])?array_values($content['member_zhuanye']):'';
        $content['member_danwei'] = isset($content['member_danwei'])?array_values($content['member_danwei']):'';
        $content['member_fengong'] = isset($content['member_fengong'])?array_values($content['member_fengong']):'';
        $content['cate_title'] = isset($content['cate_title'])?array_values($content['cate_title']):'';
        $content['class_title'] = isset($content['class_title'])?array_values($content['class_title']):'';
        $content['p_time'] = isset($content['p_time'])?array_values($content['p_time']):'';
        $content['if_finish'] = isset($content['if_finish'])?array_values($content['if_finish']):'';
        // dump($content);die;
        $this->assign('content',$content);
        $this->assign("if_two",$info['if_two']);
        if($type==1){
            //查看打分详情
        // $dafen_list = Db::name('dafen as df')
        //             ->leftjoin("admin a",'a.id=df.admin_id')
        //             ->where("df.sid",$info['id'])
        //             ->where("school",$this->admin['school'])
        //             ->field("df.*,a.name")
        //             ->select();
                     $dafen_list = Db::name('admin')
                        // ->Leftjoin('dafen df','a.id=df.admin_id')
                        // ->where("df.sid",$info['id'])
                        ->where("school",$this->admin['school'])
                        ->where('rid',12)
                        ->where('z_type','in',[$info['if_inportant'],'全部课题'])
                        // ->field("df.*,a.name")
                        ->select();
            foreach($dafen_list as $key=>$val){
                $if_exit = Db::name('dafen')->where('sid',$info['id'])->where('admin_id',$val['id'])->find();
                if($if_exit){
                    $dafen_list[$key]['fen'] = $if_exit['fen'];
                }else{
                    $dafen_list[$key]['fen'] = 0;
                }
                
            }            
        $this->assign("dafen",$dafen_list);
        return $this->fetch('kanfen');
        }
        return $this->fetch();
    }
    public function agreen(){
        $id = input("id");
        
        if(Request::isPost()){
            $data = input("post.");
            $sq = Db::name("shenqing")->where("id",$id)->find();
            //查看学校通过的命额
             //1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
            $count = Db::name('shenqing')->where('cid',$sq['cid'])->where("status",'in','3,6')->where('school_id',$sq['school_id'])->count();
            //查看可以通过名额
            if($this->admin['area']==52){
                $cate = "直属校";
                // $data_sq['status'] = 1;
                
            }elseif($this->admin['area']==2){
                $cate = "普通高校、高职院校";
                // $data_sq['status'] = 1;
            }
          
            $member_class = Db::name("member_class")->where("cid",$sq['cid'])->where("type",$cate)->find();
            if($member_class['num'] <=$count){
               ajaxMsg(0,'通过人员已到上限');
            }
            if($sq['message']){
               $info['message'] = $sq['message']."<br/>学校意见:".$data['message']; 
            }else{
                $info['message'] = "学校意见:".$data['message'];
            }
            $info['status'] = 3;
            $res = Db::name("shenqing")->where("id",$data['id'])->update($info);
            if($res){
                ajaxMsg(1,'审核成功');
            }
            ajaxMsg(0,'网络错误,审核失败');
        }
        $this->assign("id",$id);
        return $this->fetch();
    }
    public function disagreen(){
        $id = input("id");
        
        if(Request::isPost()){
            $data = input("post.");
            $sq = Db::name("shenqing")->where("id",$id)->find();

            
            if($sq['message']){
               $info['message'] = $sq['message']."<br/>学校意见:".$data['message']; 
            }else{
                $info['message'] = "学校意见:".$data['message'];
            }
            $info['if_re'] = $data['if_re'];
            $info['status'] = 5;
            $res = Db::name("shenqing")->where("id",$data['id'])->update($info);
            if($res){
                ajaxMsg(1,'拒绝成功');
            }
            ajaxMsg(0,'网络错误,审核失败');
        }
        $this->assign("id",$id);
        return $this->fetch();
    }
}











